Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.3 v dplyr 1.0.7
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 2.0.0 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./n1_n2_cleaned_cases.rds")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.3, n = 492)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 12.89636 12.89964 12.90291 12.90616 12.90938 12.91257 12.91572 12.91881
## [9] 12.92184 12.92479 12.92767 12.93045 12.93313 12.93571 12.93816 12.94049
## [17] 12.94268 12.94473 12.94662 12.94835 12.94990 12.95127 12.95248 12.95356
## [25] 12.95451 12.95534 12.95605 12.95666 12.95716 12.95756 12.95786 12.95808
## [33] 12.95821 12.95826 12.95824 12.95816 12.95801 12.95780 12.95754 12.95723
## [41] 12.95688 12.95649 12.95607 12.95563 12.95517 12.95469 12.95420 12.95370
## [49] 12.95321 12.95272 12.95212 12.95128 12.95021 12.94892 12.94742 12.94571
## [57] 12.94381 12.94172 12.93945 12.93700 12.93440 12.93163 12.92872 12.92567
## [65] 12.92249 12.91918 12.91576 12.91223 12.90860 12.90488 12.90108 12.89720
## [73] 12.89325 12.88925 12.88519 12.88109 12.87696 12.87279 12.86861 12.86442
## [81] 12.86022 12.85603 12.85186 12.84770 12.84357 12.83948 12.83544 12.83145
## [89] 12.82752 12.82366 12.81988 12.81618 12.81258 12.80908 12.80569 12.80242
## [97] 12.79927 12.79626 12.79339 12.79013 12.78598 12.78104 12.77538 12.76908
## [105] 12.76223 12.75490 12.74718 12.73915 12.73089 12.72247 12.71399 12.70553
## [113] 12.69715 12.68896 12.68102 12.67341 12.66623 12.65954 12.65344 12.64800
## [121] 12.64330 12.63943 12.63646 12.63449 12.63357 12.63381 12.63528 12.63807
## [129] 12.64267 12.64938 12.65790 12.66796 12.67927 12.69155 12.70452 12.71789
## [137] 12.73139 12.74473 12.75763 12.76980 12.78098 12.79087 12.80061 12.81149
## [145] 12.82340 12.83626 12.84998 12.86447 12.87963 12.89539 12.91164 12.92830
## [153] 12.94528 12.96249 12.97984 12.99724 13.01459 13.03181 13.04882 13.06551
## [161] 13.08179 13.09759 13.11281 13.12735 13.14114 13.15407 13.16606 13.17702
## [169] 13.18916 13.20452 13.22271 13.24332 13.26598 13.29030 13.31588 13.34234
## [177] 13.36929 13.39634 13.42311 13.44920 13.47422 13.49779 13.51951 13.53901
## [185] 13.55588 13.56974 13.58021 13.58689 13.58940 13.58882 13.58659 13.58282
## [193] 13.57764 13.57115 13.56347 13.55473 13.54503 13.53449 13.52324 13.51137
## [201] 13.49902 13.48630 13.47333 13.46021 13.44708 13.43262 13.41559 13.39614
## [209] 13.37444 13.35066 13.32495 13.29749 13.26844 13.23797 13.20623 13.17340
## [217] 13.13964 13.10511 13.06999 13.03443 12.99860 12.96267 12.92680 12.89115
## [225] 12.85589 12.82119 12.78721 12.75412 12.72207 12.69125 12.66180 12.63092
## [233] 12.59615 12.55821 12.51780 12.47565 12.43247 12.38896 12.34585 12.30384
## [241] 12.26364 12.22599 12.19157 12.16111 12.13533 12.11244 12.09023 12.06872
## [249] 12.04796 12.02797 12.00880 11.99047 11.97302 11.95649 11.94091 11.92632
## [257] 11.91274 11.90022 11.88879 11.87917 11.87196 11.86696 11.86396 11.86277
## [265] 11.86319 11.86504 11.86810 11.87219 11.87711 11.88266 11.88865 11.89488
## [273] 11.90115 11.90726 11.91302 11.91824 11.92272 11.92625 11.92865 11.92972
## [281] 11.92926 11.92707 11.92594 11.92837 11.93373 11.94140 11.95077 11.96120
## [289] 11.97207 11.98277 11.99266 12.00113 12.00756 12.01131 12.01178 12.00833
## [297] 12.00212 11.99479 11.98647 11.97727 11.96731 11.95673 11.94563 11.93414
## [305] 11.92237 11.91046 11.89852 11.88667 11.87503 11.86372 11.85117 11.83588
## [313] 11.81807 11.79798 11.77583 11.75186 11.72629 11.69936 11.67129 11.64232
## [321] 11.61268 11.58259 11.55229 11.52200 11.49196 11.46240 11.43354 11.40562
## [329] 11.37887 11.35352 11.32979 11.30792 11.28814 11.27067 11.25575 11.24361
## [337] 11.23448 11.22667 11.21850 11.21022 11.20210 11.19439 11.18733 11.18119
## [345] 11.17621 11.17265 11.17077 11.17082 11.17305 11.17771 11.18521 11.19557
## [353] 11.20845 11.22354 11.24053 11.25910 11.27892 11.29969 11.32107 11.34275
## [361] 11.36442 11.38575 11.40643 11.42613 11.44655 11.46947 11.49471 11.52212
## [369] 11.55151 11.58274 11.61561 11.64998 11.68566 11.72249 11.76030 11.79893
## [377] 11.83819 11.87794 11.91799 11.95817 11.99833 12.03829 12.07787 12.11693
## [385] 12.15527 12.19274 12.22917 12.26439 12.29823 12.33052 12.36110 12.38979
## [393] 12.41957 12.45308 12.48957 12.52835 12.56868 12.60984 12.65113 12.69181
## [401] 12.73116 12.76847 12.80302 12.83409 12.86095 12.88289 12.90195 12.92059
## [409] 12.93869 12.95616 12.97287 12.98872 13.00361 13.01742 13.03004 13.04137
## [417] 13.05129 13.05971 13.06650 13.07156 13.07436 13.07458 13.07239 13.06797
## [425] 13.06152 13.05320 13.04320 13.03171 13.01890 13.00495 12.99006 12.97440
## [433] 12.95815 12.94149 12.92461 12.90769 12.89091 12.87446 12.85850 12.84324
## [441] 12.82884 12.81549 12.80337 12.79157 12.77905 12.76586 12.75204 12.73762
## [449] 12.72265 12.70715 12.69116 12.67474 12.65790 12.64069 12.62315 12.60532
## [457] 12.58723 12.56892 12.55043 12.53179 12.51305 12.49424 12.47539 12.45656
## [465] 12.43777 12.41906 12.40047 12.38204 12.36380 12.34564 12.32741 12.30911
## [473] 12.29074 12.27228 12.25375 12.23513 12.21642 12.19762 12.17872 12.15973
## [481] 12.14064 12.12144 12.10213 12.08271 12.06318 12.04353 12.02376 12.00386
## [489] 11.98384 11.96368 11.94339 11.92297
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.3, n = 492)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 12.55496 12.55394 12.55297 12.55205 12.55118 12.55036 12.54959 12.54885
## [9] 12.54815 12.54749 12.54686 12.54625 12.54568 12.54512 12.54458 12.54407
## [17] 12.54356 12.54306 12.54258 12.54209 12.54161 12.54113 12.54064 12.54015
## [25] 12.53965 12.53916 12.53867 12.53818 12.53770 12.53723 12.53677 12.53633
## [33] 12.53590 12.53549 12.53510 12.53473 12.53439 12.53408 12.53379 12.53354
## [41] 12.53332 12.53314 12.53300 12.53290 12.53284 12.53282 12.53286 12.53294
## [49] 12.53308 12.53327 12.53350 12.53377 12.53407 12.53440 12.53477 12.53518
## [57] 12.53562 12.53610 12.53661 12.53717 12.53776 12.53840 12.53908 12.53980
## [65] 12.54056 12.54137 12.54222 12.54312 12.54406 12.54505 12.54609 12.54718
## [73] 12.54832 12.54951 12.55074 12.55204 12.55338 12.55478 12.55623 12.55774
## [81] 12.55931 12.56093 12.56261 12.56435 12.56614 12.56800 12.56992 12.57190
## [89] 12.57395 12.57605 12.57823 12.58047 12.58277 12.58514 12.58758 12.59008
## [97] 12.59266 12.59531 12.59803 12.60005 12.60070 12.60009 12.59833 12.59555
## [105] 12.59186 12.58738 12.58223 12.57651 12.57036 12.56389 12.55720 12.55043
## [113] 12.54369 12.53710 12.53077 12.52481 12.51936 12.51452 12.51041 12.50715
## [121] 12.50486 12.50365 12.50364 12.50494 12.50768 12.51198 12.51818 12.52638
## [129] 12.53632 12.54773 12.56035 12.57391 12.58815 12.60280 12.61759 12.63227
## [137] 12.64657 12.66021 12.67294 12.68449 12.69673 12.71154 12.72870 12.74796
## [145] 12.76910 12.79187 12.81604 12.84138 12.86766 12.89463 12.92206 12.94971
## [153] 12.97737 13.00477 13.03170 13.05792 13.08319 13.10727 13.12994 13.15095
## [161] 13.17008 13.18708 13.20173 13.21630 13.23301 13.25157 13.27164 13.29291
## [169] 13.31507 13.33780 13.36077 13.38369 13.40622 13.42805 13.44887 13.46837
## [177] 13.48621 13.50209 13.51569 13.52669 13.53479 13.53965 13.54201 13.54284
## [185] 13.54221 13.54019 13.53683 13.53221 13.52638 13.51942 13.51139 13.50234
## [193] 13.49235 13.48149 13.46980 13.45737 13.44426 13.43052 13.41623 13.40144
## [201] 13.38623 13.37066 13.35479 13.33868 13.32088 13.30005 13.27644 13.25031
## [209] 13.22193 13.19156 13.15945 13.12586 13.09105 13.05529 13.01882 12.98192
## [217] 12.94483 12.90783 12.87116 12.83509 12.79987 12.76578 12.73305 12.70197
## [225] 12.67277 12.64574 12.61730 12.58430 12.54756 12.50791 12.46617 12.42318
## [233] 12.37976 12.33674 12.29495 12.25521 12.21835 12.18520 12.15659 12.13335
## [241] 12.11277 12.09175 12.07050 12.04925 12.02821 12.00759 11.98761 11.96849
## [249] 11.95044 11.93368 11.91842 11.90489 11.89329 11.88384 11.87715 11.87354
## [257] 11.87277 11.87464 11.87892 11.88538 11.89380 11.90397 11.91566 11.92865
## [265] 11.94271 11.95764 11.97319 11.98916 12.00531 12.02144 12.03731 12.05270
## [273] 12.06740 12.08118 12.09382 12.10509 12.11479 12.12267 12.12853 12.13213
## [281] 12.13647 12.14423 12.15477 12.16744 12.18161 12.19664 12.21187 12.22667
## [289] 12.24039 12.25240 12.26204 12.26869 12.27168 12.27038 12.26567 12.25897
## [297] 12.25052 12.24053 12.22923 12.21684 12.20358 12.18966 12.17531 12.16076
## [305] 12.14622 12.13191 12.11805 12.10487 12.09041 12.07279 12.05230 12.02925
## [313] 12.00392 11.97663 11.94768 11.91736 11.88597 11.85382 11.82121 11.78843
## [321] 11.75579 11.72359 11.69212 11.66169 11.63261 11.60516 11.57965 11.55638
## [329] 11.53566 11.51777 11.50303 11.48908 11.47364 11.45708 11.43977 11.42210
## [337] 11.40445 11.38718 11.37069 11.35533 11.34150 11.32957 11.31992 11.31292
## [345] 11.30895 11.30672 11.30478 11.30324 11.30223 11.30187 11.30229 11.30361
## [353] 11.30595 11.30944 11.31419 11.32034 11.32800 11.33730 11.34835 11.36188
## [361] 11.37838 11.39766 11.41953 11.44379 11.47027 11.49875 11.52906 11.56101
## [369] 11.59440 11.62905 11.66475 11.70133 11.73858 11.77633 11.81438 11.85253
## [377] 11.89060 11.92840 11.96574 12.00242 12.03825 12.07305 12.10662 12.13877
## [385] 12.16932 12.19806 12.22481 12.25192 12.28154 12.31320 12.34643 12.38074
## [393] 12.41566 12.45073 12.48547 12.51940 12.55204 12.58294 12.61160 12.63756
## [401] 12.66034 12.68225 12.70570 12.73030 12.75571 12.78157 12.80750 12.83315
## [409] 12.85817 12.88218 12.90483 12.92575 12.94459 12.96099 12.97457 12.98589
## [417] 12.99574 13.00417 13.01126 13.01704 13.02157 13.02491 13.02711 13.02822
## [425] 13.02830 13.02740 13.02558 13.02289 13.01937 13.01510 13.01012 13.00448
## [433] 12.99824 12.99145 12.98417 12.97645 12.96835 12.95991 12.95120 12.94226
## [441] 12.93315 12.92367 12.91357 12.90283 12.89143 12.87934 12.86655 12.85303
## [449] 12.83877 12.82375 12.80794 12.79132 12.77387 12.75558 12.73642 12.71644
## [457] 12.69571 12.67424 12.65201 12.62903 12.60531 12.58083 12.55560 12.52962
## [465] 12.50288 12.47540 12.44716 12.41818 12.38843 12.35794 12.32668 12.29467
## [473] 12.26190 12.22838 12.19410 12.15906 12.12327 12.08673 12.04943 12.01138
## [481] 11.97257 11.93301 11.89270 11.85164 11.80983 11.76726 11.72395 11.67988
## [489] 11.63507 11.58951 11.54319 11.49613
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.3, n = 492)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 11.76573 11.77394 11.78213 11.79030 11.79841 11.80646 11.81444 11.82232
## [9] 11.83009 11.83774 11.84525 11.85261 11.85980 11.86681 11.87362 11.88021
## [17] 11.88657 11.89269 11.89855 11.90414 11.90944 11.91443 11.91910 11.92344
## [25] 11.92742 11.93104 11.93428 11.93712 11.93956 11.94161 11.94331 11.94470
## [33] 11.94578 11.94657 11.94709 11.94735 11.94738 11.94717 11.94677 11.94616
## [41] 11.94539 11.94446 11.94338 11.94218 11.94087 11.93947 11.93799 11.93646
## [49] 11.93487 11.93327 11.93165 11.93004 11.92845 11.92690 11.92540 11.92398
## [57] 11.92264 11.92089 11.91825 11.91476 11.91046 11.90538 11.89958 11.89307
## [65] 11.88592 11.87815 11.86981 11.86093 11.85157 11.84174 11.83151 11.82090
## [73] 11.80995 11.79871 11.78722 11.77551 11.76363 11.75161 11.73950 11.72733
## [81] 11.71515 11.70299 11.69090 11.67891 11.66707 11.65540 11.64397 11.63279
## [89] 11.62193 11.61140 11.60126 11.59154 11.58228 11.57353 11.56532 11.55769
## [97] 11.55069 11.54434 11.53870 11.53381 11.52969 11.52640 11.52397 11.52244
## [105] 11.52185 11.52224 11.52304 11.52370 11.52428 11.52486 11.52551 11.52631
## [113] 11.52732 11.52862 11.53028 11.53236 11.53495 11.53812 11.54193 11.54646
## [121] 11.55178 11.55796 11.56508 11.57320 11.58240 11.59275 11.60432 11.61719
## [129] 11.63342 11.65455 11.67984 11.70856 11.73999 11.77339 11.80803 11.84317
## [137] 11.87810 11.91206 11.94435 11.97421 12.00092 12.02375 12.04547 12.06920
## [145] 12.09476 12.12198 12.15068 12.18068 12.21181 12.24388 12.27671 12.31014
## [153] 12.34397 12.37804 12.41216 12.44616 12.47985 12.51307 12.54562 12.57734
## [161] 12.60804 12.63755 12.66569 12.69228 12.71714 12.74009 12.76097 12.77957
## [169] 12.79731 12.81561 12.83432 12.85332 12.87248 12.89165 12.91072 12.92954
## [177] 12.94797 12.96590 12.98318 12.99969 13.01528 13.02983 13.04319 13.05525
## [185] 13.06586 13.07490 13.08222 13.08770 13.09120 13.09337 13.09488 13.09570
## [193] 13.09577 13.09503 13.09343 13.09092 13.08745 13.08296 13.07740 13.07071
## [201] 13.06284 13.05375 13.04337 13.03165 13.01855 13.00303 12.98426 12.96247
## [209] 12.93788 12.91070 12.88117 12.84950 12.81592 12.78065 12.74391 12.70592
## [217] 12.66691 12.62710 12.58671 12.54596 12.50508 12.46429 12.42381 12.38386
## [225] 12.34467 12.30646 12.26945 12.23387 12.19993 12.16785 12.13787 12.10618
## [233] 12.06946 12.02863 11.98463 11.93840 11.89088 11.84299 11.79568 11.74987
## [241] 11.70651 11.66652 11.63085 11.60043 11.57619 11.55546 11.53504 11.51501
## [249] 11.49549 11.47657 11.45837 11.44098 11.42450 11.40905 11.39471 11.38161
## [257] 11.36983 11.35948 11.35067 11.34502 11.34383 11.34672 11.35329 11.36315
## [265] 11.37590 11.39117 11.40856 11.42767 11.44812 11.46951 11.49146 11.51357
## [273] 11.53545 11.55672 11.57697 11.59583 11.61289 11.62777 11.64008 11.64942
## [281] 11.65541 11.65765 11.65966 11.66480 11.67244 11.68195 11.69272 11.70412
## [289] 11.71552 11.72629 11.73581 11.74346 11.74861 11.75063 11.74891 11.74280
## [297] 11.73029 11.71050 11.68457 11.65363 11.61881 11.58125 11.54207 11.50240
## [305] 11.46339 11.42616 11.39184 11.36157 11.33648 11.31770 11.30175 11.28453
## [313] 11.26616 11.24681 11.22662 11.20574 11.18432 11.16251 11.14045 11.11830
## [321] 11.09621 11.07432 11.05278 11.03175 11.01137 10.99178 10.97315 10.95561
## [329] 10.93932 10.92442 10.91107 10.89941 10.88960 10.88177 10.87609 10.87270
## [337] 10.87174 10.87299 10.87600 10.88060 10.88662 10.89389 10.90223 10.91147
## [345] 10.92145 10.93198 10.94289 10.95402 10.96519 10.97622 10.98861 11.00369
## [353] 11.02113 11.04059 11.06170 11.08413 11.10753 11.13154 11.15584 11.18006
## [361] 11.20386 11.22689 11.24881 11.26927 11.28951 11.31096 11.33353 11.35714
## [369] 11.38168 11.40708 11.43325 11.46010 11.48755 11.51550 11.54388 11.57258
## [377] 11.60153 11.63063 11.65980 11.68896 11.71801 11.74686 11.77544 11.80364
## [385] 11.83139 11.85860 11.88518 11.91104 11.93609 11.96025 11.98343 12.00554
## [393] 12.02868 12.05465 12.08292 12.11298 12.14430 12.17635 12.20861 12.24055
## [401] 12.27165 12.30139 12.32924 12.35468 12.37718 12.39621 12.41365 12.43158
## [409] 12.44978 12.46806 12.48621 12.50402 12.52128 12.53778 12.55332 12.56770
## [417] 12.58070 12.59212 12.60175 12.60939 12.61495 12.61858 12.62040 12.62054
## [425] 12.61913 12.61630 12.61218 12.60690 12.60058 12.59335 12.58535 12.57670
## [433] 12.56754 12.55798 12.54815 12.53820 12.52824 12.51840 12.50882 12.49961
## [441] 12.49092 12.48287 12.47558 12.46841 12.46065 12.45233 12.44347 12.43410
## [449] 12.42427 12.41399 12.40330 12.39223 12.38080 12.36906 12.35702 12.34473
## [457] 12.33220 12.31948 12.30659 12.29356 12.28042 12.26720 12.25394 12.24065
## [465] 12.22739 12.21416 12.20101 12.18797 12.17506 12.16218 12.14921 12.13614
## [473] 12.12297 12.10970 12.09633 12.08285 12.06926 12.05556 12.04175 12.02783
## [481] 12.01379 11.99963 11.98535 11.97095 11.95642 11.94177 11.92698 11.91206
## [489] 11.89701 11.88183 11.86650 11.85103
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")